package com.daman
import xitrum.Action
/**
 * Created by daman on 2015/5/12.
 */
trait AppAction extends Action{
  beforeFilter {
    log.info("I run therefore I am")
  }
  aroundFilter { action =>
    val begin = System.currentTimeMillis()
    action()
    val end   = System.currentTimeMillis()
    log.info("The action took " + (end - begin) + " [ms]")
  }

  def dropEmpty(data:Map[String,String]) = {
    data.filterNot(e=>e._2.trim().isEmpty)
  }

  def getPost( f:(Map[String,String])=>Any)  = {
    val fields = requestContentJson[Map[String, String]]
    fields match {
      case Some(field) =>
        val input = dropEmpty(field)
        f(input)
      case None => respondJson(Respond.success("please input data"))
    }
  }

  def respondOK(data:Any): Unit ={
    respondJson(Map("errorcode"->0,"errorinfo"->"","data"->data))
  }
  
}
